﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Bubbels.Helper;

namespace Bubbels.Controls
{
    /// <summary>
    /// Interaction logic for GameBoard.xaml
    /// </summary>
    public partial class Board : UserControl
    {
        public Board()
        {
            InitializeComponent();
            this.DataContextChanged += (object sender, DependencyPropertyChangedEventArgs e) => {
                (this.Resources["positionConverter"] as PositionConverter).Row = (e.NewValue as BubbelsVM).Row;
                (this.Resources["positionConverter"] as PositionConverter).Col = (e.NewValue as BubbelsVM).Column;
            };
            this.SizeChanged += (object sender, SizeChangedEventArgs evt) =>
            {

                (this.Resources["positionConverter"] as PositionConverter).Height = this.Height;
                (this.Resources["positionConverter"] as PositionConverter).Width = this.Width;
            };
            
        }


        private void Label_MouseEnter(object sender, MouseEventArgs e)
        {
            var originalSource = e.OriginalSource;
            var vm = (originalSource as FrameworkElement).DataContext as BallVM;
            if (vm.Removed)
            {
                return;
            }
            (this.DataContext as BubbelsVM).ColorUnion(vm.ColumnWithDelta, vm.RowWithDelta);
           
            int count = (this.DataContext as BubbelsVM).GetUnion(vm.ColumnWithDelta, vm.RowWithDelta).Count;
            ShowScoreTips(count*count);
        }

        private void ShowScoreTips(int score)
        { 
        }

        public event Action GameOver = delegate { };
        private object locked = new object();
        private void Label_MouseUp(object sender, MouseButtonEventArgs e)
        {
            (this.DataContext as BubbelsVM).UnionScore = 0;            
            var originalSource = e.OriginalSource;
            var vm = (originalSource as FrameworkElement).DataContext as BallVM;
            if ((this.DataContext as BubbelsVM).GetUnion(vm.ColumnWithDelta, vm.RowWithDelta).Count <= 1)
            {
                return;
            }
            var balls = (this.DataContext as BubbelsVM).ClearUnion(vm.ColumnWithDelta, vm.RowWithDelta);
            if ((this.DataContext as BubbelsVM).IsGameOver())
            {
                GameOver();
            }
        }

        private void DummyAction(object sender, MouseButtonEventArgs e)
        {
            
        }

        private void Storyboard_Completed(object sender, EventArgs e)
        {
           // Clear all removed balls from view model
            (this.DataContext as BubbelsVM).GabageCollect();

            // Trigger balls movement(move right, move down)
            (this.DataContext as BubbelsVM).ResetDelta();
        }

    }
}
